/**************************************************************************
 *
 * Copyright (C) 2007 University of California, Los Angeles.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 *************************************************************************/

package edu.ucla.cs.typecast.rmi;

import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Collection;

import edu.ucla.cs.typecast.net.EndPoint;
import edu.ucla.cs.typecast.net.EndPointListener;
import edu.ucla.cs.typecast.net.EndPointListenerFactory;
import edu.ucla.cs.typecast.net.NetUtil;
import edu.ucla.cs.typecast.net.TypeCastConstants;
import edu.ucla.cs.typecast.rmi.server.TypeCastServiceRegistrar;
import edu.ucla.cs.typecast.rmi.server.TypeCastObjectManager;
import edu.ucla.cs.typecast.util.TypeUtil;

public class TypeCastServer {
	private EndPoint endPoint; 
	private TypeCastServiceRegistrar serviceRegistrar; 
		
	public TypeCastServer() throws IOException, UnknownHostException {
		this(TypeCastConstants.DEFAULT_RMI_ADDRESS, TypeCastConstants.DEFAULT_RMI_PORT);
	}

	public TypeCastServer(int port) throws IOException, UnknownHostException {
		this(TypeCastConstants.DEFAULT_RMI_ADDRESS, port);
	}

	public TypeCastServer(String address, int port) throws IOException, UnknownHostException {
		this(InetAddress.getByName(address), port);
	}

	public TypeCastServer(InetAddress address, int port) throws IOException{
		this(new EndPoint(address, port)); 
	}

	public TypeCastServer(EndPoint endPoint) throws IOException { 
		if (endPoint != null && endPoint.getPort() > 0) { 
			this.endPoint = endPoint; 
			serviceRegistrar = TypeCastObjectManager.getInstance().getServiceRegistrar(endPoint); 		
		} else { 
			if (endPoint == null)  { 
				endPoint = new EndPoint(NetUtil.getLocalHost(), 0); 
			}
			EndPointListener endPointListener = EndPointListenerFactory.createEndPointListener(endPoint); 
			endPoint = endPointListener.activate(endPoint); 
			this.endPoint = endPoint; 
			serviceRegistrar = TypeCastObjectManager.getInstance().getServiceRegistrar(endPoint, endPointListener); 			
		}
	}

	/*
	public TypeCastServer(EndPoint endPoint, EndPointListener endPointListener) { 
		this.endPoint = endPoint; 
		serviceRegistrar = TypeCastObjectManager.getInstance().getServiceRegistrar(endPoint, endPointListener); 
	}
	*/
	
	public EndPoint getEndPoint() { 
		return this.endPoint; 
	}
	
	public void export(Class[] types, Object typeCastService)
			throws IOException {
		serviceRegistrar.exportService(types, typeCastService); 
	}

	public <T> void export(Class<T> type, T typeCastService) throws IOException {
		export(new Class[] { type }, typeCastService);
	}

	public void export(Object typeCastService) throws IOException {
		export(TypeUtil.getInterfaces(typeCastService.getClass()), typeCastService);
	}
	
	public void unexport(Object typeCastService)  {
		try { 
			serviceRegistrar.unexportService(typeCastService); 
		} catch(Throwable t) {}
	}
	
	public Collection getServiceObjects(Class type) { 
		return serviceRegistrar.getServices(type); 
	}
}
